package com.htu.courseService.courseManage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htu.courseService.common.enums.ErrorCodeEnum;
import com.htu.courseService.common.enums.RedisCachePrefix;
import com.htu.courseService.common.exception.BusinessException;
import com.htu.courseService.common.util.UserUtil;
import com.htu.courseService.courseContents.dao.DirectoriesMapper;
import com.htu.courseService.courseContents.entity.Directories;
import com.htu.courseService.courseContents.vo.DirectoriesDto;
import com.htu.courseService.courseManage.dao.CoursesMapper;
import com.htu.courseService.courseManage.entity.Courses;
import com.htu.courseService.courseManage.service.CoursesService;
import com.htu.courseService.courseManage.vo.CourseQuery;
import com.htu.courseService.courseManage.vo.CoursesDetail;
import com.htu.courseService.courseManage.vo.CoursesDto;
import com.htu.courseService.user.vo.UserInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class CoursesServiceImpl implements RedisCachePrefix, CoursesService {
    private final CoursesMapper coursesMapper;

    private final DirectoriesMapper directoriesMapper;
    @Override
    public void saveCourses(CoursesDto coursesDto) {
        String courseName = coursesDto.getCourseName();
        if(StrUtil.isEmpty(courseName)){
            throw new BusinessException(ErrorCodeEnum.COURSE_NAME_NOT_FOUND);
        }
        UserInfo userInfo = UserUtil.getUserInfo();
        Courses courses = BeanUtil.copyProperties(coursesDto,Courses.class);
        courses.setCreatedAt(new Date());
        courses.setCreatedBy(userInfo.getId());
        int count = coursesMapper.insert(courses);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.INSERT_FAIL);
        }
    }

    @Override
    public void deleteCourses(Integer coursesId) {
        int count = coursesMapper.deleteById(coursesId);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.DELETE_FAIL);
        }
    }

    @Override
    public IPage<CoursesDto> getCoursePage(CourseQuery courseQuery) {
        Page<Courses> page = new Page<>(courseQuery.getPageNum(),courseQuery.getPageSize());
        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();
        UserInfo userInfo = UserUtil.getUserInfo();
        Map<String,List<String>> rolesAndPermissions = userInfo.getRolesAndPermissions();
//        if(rolesAndPermissions.containsKey("teacher")){
//            wrapper.eq(Courses::getCreatedBy,userInfo.getId());
//        }
        if(StrUtil.isNotEmpty(courseQuery.getCourseName())){
            wrapper.like(Courses::getCourseName,courseQuery.getCourseName());
        }
        if(StrUtil.isNotEmpty(courseQuery.getCourseType())){
            wrapper.eq(Courses::getCourseType,courseQuery.getCourseType());
        }
        Long count = coursesMapper.selectCount(wrapper);
        IPage<Courses> courseList = coursesMapper.selectPage(page,wrapper);
        List<CoursesDto> coursesDtos = BeanUtil.copyToList(courseList.getRecords(), CoursesDto.class);
        IPage iPage = new Page();
        iPage.setTotal(count);
        iPage.setCurrent(courseQuery.getPageNum());
        iPage.setRecords(coursesDtos);
        iPage.setSize(coursesDtos.size());
        return iPage;
    }

    @Override
    public void updateCourseInfo(CoursesDto coursesDto) {
        Courses courses = BeanUtil.copyProperties(coursesDto,Courses.class);
        int count = coursesMapper.updateById(courses);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.UPDATE_FAIL);
        }
    }

    @Override
    public CoursesDetail getCourseDetail(Integer courseId) {
        CoursesDetail courseDetail = coursesMapper.getCourseDetail(courseId);
        LambdaQueryWrapper<Directories> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Directories::getCourseId,courseId);
        List<Directories> directoriesList = directoriesMapper.selectList(wrapper);
        if(null != directoriesList){
            List<DirectoriesDto> directoriesDtoList = BeanUtil.copyToList(directoriesList, DirectoriesDto.class);
            courseDetail.setDirectoriesDtoList(directoriesDtoList);
        }
        return courseDetail;
    }
}
